package main

import (
	"fmt"
	"log"
	"os"
	"sync"
	"time"
)

var mu sync.Mutex

func createOutputFile(outfile string) (*os.File, error) {
	return os.Create(outfile)
}

func recordDetectResult(info string, f *os.File) {
	mu.Lock()
	f.WriteString(info + "\n")
	mu.Unlock()
}

func main() {

	f, err := createOutputFile("all.txt")

	var wg sync.WaitGroup

	if err != nil {
		log.Fatalf("create error: %v\n", err.Error())
	}

	for i := 1; i <= 10; i++ {
		wg.Add(1)
		go func(n int) {
			defer wg.Done()
			s := fmt.Sprintf("%s write num %d", time.Now(), i)
			recordDetectResult(s, f)
		}(i)
	}

	log.Printf("ok")
	wg.Wait()
}
